package net.llz.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.llz.enums.BizCodeEnum;
import net.llz.exception.BizException;
import net.llz.request.LockCouponRecordRequest;
import net.llz.service.CouponRecordService;
import net.llz.util.JsonData;
import net.llz.vo.CouponRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Let-凡
 * @since 2023-01-15
 */

@Api(tags = "领卷记录")
@RestController
@RequestMapping("/api/coupon_record/v1")
public class CouponRecordController {


    @Autowired
    private CouponRecordService couponRecordService;

    /**
     * 分页查询分页记录
     * @param page
     * @param size
     * @return
     */
    @ApiOperation("领卷查询记录")
    @GetMapping("page")
    public JsonData page(@ApiParam(value = "当前页") @RequestParam(value = "page",defaultValue = "1") int page,
                        @ApiParam(value = "每页多少数据") @RequestParam(value = "size",defaultValue = "5" )int size){

        Map<String,Object>  pageResult=  couponRecordService.page(page,size);

        return JsonData.buildSuccess(pageResult);
    }

    @ApiOperation("查看领卷详情")
    @GetMapping("detail/{record_id}")
    public JsonData getCouponRecordDetail(@ApiParam(value = "领卷详情") @PathVariable("record_id") long recordId){

        CouponRecordVO couponRecordVO = couponRecordService.findById(recordId);

        return couponRecordVO == null ? JsonData.buildResult(BizCodeEnum.COUPON_NO_EXITS) : JsonData.buildSuccess(couponRecordVO);

    }


    @ApiOperation("rpc-锁定，优惠卷记录")
    @PostMapping("lock_records")
    public JsonData lockCouponRecords(@ApiParam("优惠卷锁定请求对象") @RequestBody LockCouponRecordRequest recordRequest) throws BizException {

        JsonData jsonData = couponRecordService.lockCouponRecords(recordRequest);

        return JsonData.buildSuccess();
    }
}

